package com.yupi.springbootinit.job.once;

import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName InsertUsers
 * @Description
 * @Author junjun
 * @Date 2023/5/25 16:26
 * @Version V1.0
 */
//@Component
public class InsertUsers implements CommandLineRunner {
    @Resource
    private UserService userService;
    String[] genders = {"男", "女"};
    String[] choices = {"小姐姐", "小哥哥", "亲爱的", "老铁", "兄弟", "姐姐", "大佬"};
    String[] jobs = {"程序员", "设计师", "记者", "老师", "医生", "律师", "音乐家"};
    String[] hobbies = {
            "爱好旅游，喜欢到处看看新鲜事物。",
            "平时喜欢看书，尤其是历史类的书籍。",
            "爱好健身，每周至少去健身房三次。",
            "喜欢听音乐，尤其是古典音乐。",
            "平时喜欢画画，自认为是个不错的画家。",
            "爱好摄影，喜欢用相机记录美好瞬间。",
            "平时喜欢玩游戏，电脑和手机都少不了。"
    };
    /**
     * 批量插入用户
     */
    final String[] roles = new String[]{
            "admin", "user", "ban"
    };
    final String[][] tags = new String[][]{
            {"java", "python", "c++","go"},
            {"大一","大二","大三","大四"},
            {"男","女"},
            {"乒乓球","篮球", "网球"}

    };
    final int BATCH_SIZE = 1000;

    @Override
    public void run(String... args) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<User> list = new ArrayList<>();
        for (int i = 0; i < BATCH_SIZE; i++) {
            User user = new User();
            //随机生成用户名
            String name = "";
            for (int j = 0; j < 10; j++) {
                name += (int) (Math.random() * 10);
            }
            user.setUserName(name);
            //随机生成密码，英文加数字加特殊字符
            String password = "";
            for (int j = 0; j < 10; j++) {
                password += (int) (Math.random() * 10);
            }
            user.setUserPassword(password);
            //随机生成账号英文加数字
            String account = "";
            for (int j = 0;j<10;j++){
                int a= (int)Math.random()*3;
                if (a==0){
                    account+=(char)((int)(Math.random()*26)+97);
                }else if (a==1){
                    account+=(char)((int)(Math.random()*26)+65);
                }else {
                    account+=(int)(Math.random()*10);
                }
            }
            //随机生成手机号要求都为数字
            String phone = "1";
            for (int j = 0; j < 10; j++) {
                phone += (int) (Math.random() * 10);
            }
            user.setUserPhone(phone);
            //随机生成邮箱
            String email = "";
            for (int j = 0; j < 10; j++) {
                email += (int) (Math.random() * 10);
            }
            email += "@qq.com";
            user.setUserEmail(email);
            user.setUserAvatar("https://th.bing.com/th?id=OIP.N8I1jxISX-UoMvRRidkUnAAAAA&w=250&h=250&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2");
            //随机生成tag
            String tag = "[";
            for (int j = 0; j < tags.length; j++) {
                tag+="\"";
                tag += tags[j][(int) (Math.random() * tags[j].length)];
                tag+="\"";
                if (j != tags.length - 1) {
                    tag += ",";
                }
            }
            tag += "]";
            user.setTags(tag);
            //随机生成role
            user.setUserRole(roles[(int) (Math.random() * 3)]);
            //随机生成性别
            user.setUserGender(genders[(int) (Math.random() * 2)]);
            //随机生成个人简介
            user.setUserProfile("我是" + choices[(int) (Math.random() * 7)] + "，是一名" + jobs[(int) (Math.random() * 7)] + "，" + hobbies[(int) (Math.random() * 7)]);
            //随机生成账号
            user.setUserAccount(account);
            list.add(user);
        }
        userService.saveBatch(list,BATCH_SIZE/10);
        stopWatch.stop();
        long totalTimeMillis = stopWatch.getTotalTimeMillis();
        System.out.println("插入" + BATCH_SIZE + "条数据，耗时：" + totalTimeMillis + "ms");

    }
}
